package java6681.lesson08;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Junichiro
 * @description
 * @date 2021-04-05 23:55
 **/

public class ListSort6681Controller {
    @FXML
    private RadioButton rb1;

    @FXML
    private RadioButton rb2;

    @FXML
    private RadioButton rb3;

    @FXML
    private RadioButton rb4;

    @FXML
    private ListView<String> lvShow;

    @FXML
    private void initialize() {
        btnTest();
        btnSecond();
        btnThree();
        btnFour();
    }

    /**
     * 功能：按成绩降序-Comparator的静态方法comparingInt/默认方法reversed
     */
    public void btnTest() {
        rb1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if (rb1.isSelected()) {
                    ObservableList<String> items = lvShow.getItems();

                    /*按成绩降序*/
                    List<Course6681> list = starting();

                    /*先清空再显示*/
                    lvShow.getItems().clear();

                    list.sort(Comparator.comparingInt(Course6681::getScore).reversed());

                    for (Course6681 grade : list) {
                        items.addAll(String.valueOf(grade));
                    }

                }
            }
        });
    }

    /**
     * 功能：按学期升序-Comparator的静态方法comparing
     */
    public void btnSecond() {
        rb2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                ObservableList<String> items = lvShow.getItems();
                List<Course6681> list = starting();
                lvShow.getItems().clear();
                list.sort(Comparator.comparing(Course6681::getTerm));
                for (Course6681 course6681 : list) {
                    items.add(String.valueOf(course6681));
                }
            }
        });
    }

    /**
     * 功能：按学分升序-Lambda表达式
     */
    public void btnThree() {
        rb3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                ObservableList<String> items = lvShow.getItems();
                List<Course6681> list = starting();
                lvShow.getItems().clear();

                list.sort((o1, o2) -> o2.getCredit()-o1.getCredit());
                for (Course6681 course6681 : list) {
                    items.add(String.valueOf(course6681));
                }
            }
        });
    }

    /**
     * 功能：按学期升序、学分降序
     * Comparator的静态方法comparing、Comparator的默认方法thenComparing
     */
    public void btnFour() {
        rb4.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                ObservableList<String> items = lvShow.getItems();
                lvShow.getItems().clear();
                List<Course6681> list = starting();
                list.sort(Comparator.comparing(Course6681::getTerm).reversed().
                        thenComparingDouble(Course6681::getCredit).reversed());
                for (Course6681 course6681 : list) {
                    items.add(String.valueOf(course6681));
                }
            }
        });
    }


    /**
     * 功能：初始化Course6681对象
     *
     * @return list 返回 Course6681对象
     */
    public static List<Course6681> starting() {
        List<Course6681> list = new ArrayList<>();
        Course6681 c1 = new Course6681("Java", 3, 89, "1下", "A001");
        Course6681 c2 = new Course6681("Python", 2, 92, "2上", "A002");
        Course6681 c3 = new Course6681("C++", 2, 78, "2下", "A003");
        Course6681 c4 = new Course6681("HTML+CSS", 2, 69, "3上", "A004");
        Course6681 c5 = new Course6681("计算机网络", 4, 79, "1上", "A005");
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        list.add(c5);
        return list;
    }
}
